'''
Created on Jul 14, 2012

@author: catapult
'''

import pygame
import time
import random
import button
import move
import gui
import pokemon
import turnInfo

ButtonBoxScreenWidth = 142
ButtonBoxScreenHeight = 375
white = (255, 255, 255)
black = (0, 0 ,0)


class ButtonBox:
    
    def __init__(self, player):
        self.player = player
        self.buttonBoxScreen = pygame.Surface([ButtonBoxScreenWidth, ButtonBoxScreenHeight])
        self.buttonBoxScreen.fill([127, 127, 127])
        self.buttonList = []
        pygame.draw.line(self.buttonBoxScreen, black, [0, 0], [0, 450])
        self.setMode(1)
        self.active = False
        self.forcePoke = False
        
    def forcePokeSwitch(self):
        self.forcePoke = True
        self.setMode(3)
        
    def inactive(self):
        self.active = False
        
    def setActive(self):
        self.active = True
        
    def display(self):
        self.buttonBoxScreen.fill([127, 127, 127])
        if(self.active):
            if(self.mode == 1):
                return self.drawMenuButtons()
            elif(self.mode == 2):
                return self.drawAttackButtons()
            elif(self.mode == 3):
                return self.drawPokeButtons()
            elif(self.mode == 4):
                return self.confirmPokeSwitch(self.clicked)
        else:
            return self.buttonBoxScreen
    
    def drawMenuButtons(self):
        self.attackButton = button.AttackChoiceButton()
        self.pokeButton = button.PokeChoiceButton()
        self.itemButton = button.ItemChoiceButton()
        self.fleeButton = button.FleeChoiceButton()
        self.rectList = [self.attackButton.rect, self.pokeButton.rect, self.itemButton.rect, self.fleeButton.rect]
        self.buttonBoxScreen.blit(self.attackButton.image, [9, 31])
        self.attackButton.rect.x = gui.SCREEN_WIDTH - 142 + 9
        self.attackButton.rect.y = 31
        self.buttonBoxScreen.blit(self.pokeButton.image, [9, 117])
        self.pokeButton.rect.x = gui.SCREEN_WIDTH - 142 + 9
        self.pokeButton.rect.y = 117
        self.buttonBoxScreen.blit(self.itemButton.image, [9, 203])
        self.itemButton.rect.x = gui.SCREEN_WIDTH - 142 + 9
        self.itemButton.rect.y = 203
        self.buttonBoxScreen.blit(self.fleeButton.image, [9, 289])
        self.fleeButton.rect.x = gui.SCREEN_WIDTH - 142 + 9
        self.fleeButton.rect.y = 289
        return self.buttonBoxScreen
    
    def drawPokeButtons(self):
        self.buttonBoxScreen.fill([127, 127, 127])
        self.rectList = []
        self.poke = []
        for n in range(0, 6):
            if(n < self.player.activePoke):
                self.poke.append(button.PokeButton(self.player.pokemon[n]))
                self.poke[n].rect.x = gui.SCREEN_WIDTH - 142 + 9
                self.poke[n].rect.y = 6 + n * 60
            elif(n > self.player.activePoke):
                self.poke.append(button.PokeButton(self.player.pokemon[n]))
                self.poke[n-1].rect.x = gui.SCREEN_WIDTH - 142 + 9
                self.poke[n-1].rect.y = 6 + (n-1) * 60
                
        self.poke.append(button.BackButton())
        self.poke[5].rect.x = gui.SCREEN_WIDTH - 142 + 9
        self.poke[5].rect.y = 6 + 300
                
        for n in range(0, 6):
            self.rectList.append(self.poke[n].rect)
            self.buttonBoxScreen.blit(self.poke[n].image, [self.poke[n].rect.x + 142 - gui.SCREEN_WIDTH, self.poke[n].rect.y])
        
        return self.buttonBoxScreen
    
    def drawAttackButtons(self):
        self.buttonBoxScreen.fill([127, 127, 127])
        self.rectList = []
        self.att = []
        self.att.append(button.AttackButton(self.player.pokemon[self.player.activePoke].move[0]))
        self.att.append(button.AttackButton(self.player.pokemon[self.player.activePoke].move[1]))
        self.att.append(button.AttackButton(self.player.pokemon[self.player.activePoke].move[2]))
        self.att.append(button.AttackButton(self.player.pokemon[self.player.activePoke].move[3]))
        
        for n in range(0, 4):
            self.rectList.append(self.att[n].rect)
        
        self.att[0].rect.x = gui.SCREEN_WIDTH - 142 + 9
        self.att[0].rect.y = 16
        self.att[1].rect.x = gui.SCREEN_WIDTH - 142 + 9
        self.att[1].rect.y = 89
        self.att[2].rect.x = gui.SCREEN_WIDTH - 142 + 9
        self.att[2].rect.y = 161
        self.att[3].rect.x = gui.SCREEN_WIDTH - 142 + 9
        self.att[3].rect.y = 233
        
        self.backButton = button.BackButton()
        self.rectList.append(self.backButton.rect)
        
        self.backButton.rect.x = gui.SCREEN_WIDTH - 142 + 9
        self.backButton.rect.y = 305
        
        for n in range(0, 4):
            self.buttonBoxScreen.blit(self.att[n].image, [self.att[n].rect.x + 142 - gui.SCREEN_WIDTH, self.att[n].rect.y])
        self.buttonBoxScreen.blit(self.backButton.image, [9, 305])
        
        return self.buttonBoxScreen      
    
    def setMode(self, mode):
        self.mode = mode
        if(mode == 1):
            self.rectList = []
            self.drawMenuButtons()
        elif(mode == 2):
            self.rectList = []
            self.drawAttackButtons()
        elif(mode == 3):
            self.rectList = []
            self.drawPokeButtons()
        elif(mode == 4):
            self.rectList = []
            
    def confirmPokeSwitch(self, clicked):
        
        self.buttonBoxScreen.fill([127, 127, 127])
        self.clicked = clicked
        pokeButton = button.PokeButton(self.player.pokemon[clicked])
        switchButton = button.SwitchButton()
        backButton = button.BackButton()
        
        self.rectList = [switchButton.rect, backButton.rect]
        
        pokeButton.rect.x = gui.SCREEN_WIDTH - 142 + 9
        pokeButton.rect.y = 75
        
        switchButton.rect.x = gui.SCREEN_WIDTH - 142 + 9
        switchButton.rect.y = 150
        
        backButton.rect.x = gui.SCREEN_WIDTH - 142 + 9
        backButton.rect.y = 225
        
        self.buttonBoxScreen.blit(pokeButton.image, [pokeButton.rect.x + 142 - gui.SCREEN_WIDTH, pokeButton.rect.y])
        self.buttonBoxScreen.blit(switchButton.image, [switchButton.rect.x + 142 - gui.SCREEN_WIDTH, switchButton.rect.y])
        self.buttonBoxScreen.blit(backButton.image, [backButton.rect.x + 142 - gui.SCREEN_WIDTH, backButton.rect.y])
        
        return self.buttonBoxScreen
        
    def checkCollisionList(self, point):
        clicked = -1
        if(self.mode == 1):
            for n in range(0, 4):
                if(self.rectList[n].collidepoint(point)):
                    print("collided")
                    clicked = n
                    break
            if(clicked == 0):
                self.setMode(2)
            elif(clicked == 1):
                self.setMode(3)
            elif(clicked == 2):
                pass
            elif(clicked == 3):
                pygame.quit()
                exit()
            return 0
        elif(self.mode == 2):
            for n in range(0, 5):
                if(self.rectList[n].collidepoint(point)):
                    print("collided")
                    clicked = n
                    break
            if(clicked == 0):
                self.setMode(1)
                return turnInfo.TurnInfo(1, 0)
            elif(clicked == 1):
                self.setMode(1)
                return turnInfo.TurnInfo(1, 1)
            elif(clicked == 2):
                self.setMode(1)
                return turnInfo.TurnInfo(1, 2)
            elif(clicked == 3):
                self.setMode(1)
                return turnInfo.TurnInfo(1, 3)
            elif(clicked == 4):
                self.setMode(1)
            return 0
        elif(self.mode == 3):
            for n in range(0, 6):
                if(self.rectList[n].collidepoint(point)):
                    print("collided")
                    clicked = n
                    break
            n = self.player.activePoke
            if(clicked == 0):
                if(n <= 0):
                    if(self.player.pokemon[1].checkAlive()):
                        self.confirmPokeSwitch(1)
                        self.setMode(4)
                else:
                    if(self.player.pokemon[0].checkAlive()):
                        self.confirmPokeSwitch(0)
                        self.setMode(4)
            elif(clicked == 1):
                if(n <= 1):
                    if(self.player.pokemon[2].checkAlive()):
                        self.confirmPokeSwitch(2)
                        self.setMode(4)
                else:
                    if(self.player.pokemon[1].checkAlive()):
                        self.confirmPokeSwitch(1)
                        self.setMode(4)
            elif(clicked == 2):
                if(n <= 2):
                    if(self.player.pokemon[3].checkAlive()):
                        self.confirmPokeSwitch(3)
                        self.setMode(4)
                else:
                    if(self.player.pokemon[2].checkAlive()):
                        self.confirmPokeSwitch(2)
                        self.setMode(4)
            elif(clicked == 3):
                if(n <= 3):
                    if(self.player.pokemon[4].checkAlive()):
                        self.confirmPokeSwitch(4)
                        self.setMode(4)
                else:
                    if(self.player.pokemon[3].checkAlive()):
                        self.confirmPokeSwitch(3)
                        self.setMode(4)
            elif(clicked == 4):
                if(n <= 4):
                    if(self.player.pokemon[5].checkAlive()):
                        self.confirmPokeSwitch(5)
                        self.setMode(4)
                else:
                    if(self.player.pokemon[4].checkAlive()):
                        self.confirmPokeSwitch(4)
                        self.setMode(4)
            elif(clicked == 5):
                if(self.forcePoke == False):
                    self.setMode(1)
            return 0
        elif(self.mode == 4):
            for n in range(0, 2):
                if(self.rectList[n].collidepoint(point)):
                    print("collided")
                    clicked = n
                    break
            if(clicked == 0):
                self.setMode(1)
                self.forcePoke = False
                return turnInfo.TurnInfo(2, self.clicked)
            elif(clicked == 1):
                self.setMode(3)
            return 0
        
                    
        